<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

</body>
<script>
    function isArrayLike( obj ) {
        if ( Object.prototype.toString.call( obj ) == '[object Array]' ) {
            return true;
        }
        var length = 'length' in obj && obj.length;
        return typeof length === 'number' && length >= 0;
    }

    function each( arr, callback ) {
        if ( isArrayLike ( arr ) ) {
            for ( var i = 0; i < arr.length; i++ ) {
                if ( callback.call( arr[ i ], arr[ i ], i ) === false ) break;
            }
        } else {
            for ( var k in arr ) {
                if ( callback.call( arr[ k ], arr[ k ], k ) === false ) break;
            }
        }
        return arr;
    }


    // each( [ 'a', 'b', 'c' ], function ( v, i ){
    //     console.log( this );
    //     console.log( i + ', ' + v );
    // });


    var o = {
        name: 'jim',
        age: 19,
        gender: '男'
    };

    each( o, function ( v, i ){
        console.log( this );
        console.log( i + ', ' + v );
    });


</script>
</html>